// META: title=test WebNN API element-wise log operation
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://www.w3.org/TR/webnn/#api-mlgraphbuilder-unary
// Compute the natural logarithm of the input tensor, element-wise.
//
// MLOperand log(MLOperand input);


const getLogPrecisionTolerance = (graphResources) => {
  const toleranceValueDict = {float32: 1 / 1024, float16: 1 / 1024};
  const expectedDataType =
      getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs);
  return {metricType: 'ATOL', value: toleranceValueDict[expectedDataType]};
};

const logTests = [
  {
    'name': 'log float32 positive 0D scalar',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [63.82542037963867],
          'descriptor': {'dimensions': [], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [4.15615177154541],
          'descriptor': {'dimensions': [], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'log float32 positive 1D constant tensor',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [
            63.82542037963867,  25.317724227905273, 96.44790649414062,
            40.91856384277344,  36.579071044921875, 57.81629943847656,
            10.057244300842285, 17.836828231811523, 50.79246520996094,
            83.860595703125,    12.065509796142578, 22.702478408813477,
            47.559814453125,    17.543880462646484, 32.65243911743164,
            20.353010177612305, 52.54472351074219,  45.608802795410156,
            30.385812759399414, 13.709558486938477, 10.396759986877441,
            50.840946197509766, 5.682034492492676,  94.02275848388672
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'},
          'constant': true
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [
            4.15615177154541,   3.2315046787261963, 4.569003105163574,
            3.7115838527679443, 3.5994763374328613, 4.057270526885986,
            2.308293104171753,  2.88126540184021,   3.927747964859009,
            4.4291558265686035, 2.4903509616851807, 3.122474193572998,
            3.861988067626953,  2.8647050857543945, 3.48591947555542,
            3.0132288932800293, 3.9616646766662598, 3.820100784301758,
            3.413975715637207,  2.618093252182007,  2.34149432182312,
            3.9287021160125732, 1.7373093366622925, 4.54353666305542
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'log float32 positive 1D tensor',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [
            63.82542037963867,  25.317724227905273, 96.44790649414062,
            40.91856384277344,  36.579071044921875, 57.81629943847656,
            10.057244300842285, 17.836828231811523, 50.79246520996094,
            83.860595703125,    12.065509796142578, 22.702478408813477,
            47.559814453125,    17.543880462646484, 32.65243911743164,
            20.353010177612305, 52.54472351074219,  45.608802795410156,
            30.385812759399414, 13.709558486938477, 10.396759986877441,
            50.840946197509766, 5.682034492492676,  94.02275848388672
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [
            4.15615177154541,   3.2315046787261963, 4.569003105163574,
            3.7115838527679443, 3.5994763374328613, 4.057270526885986,
            2.308293104171753,  2.88126540184021,   3.927747964859009,
            4.4291558265686035, 2.4903509616851807, 3.122474193572998,
            3.861988067626953,  2.8647050857543945, 3.48591947555542,
            3.0132288932800293, 3.9616646766662598, 3.820100784301758,
            3.413975715637207,  2.618093252182007,  2.34149432182312,
            3.9287021160125732, 1.7373093366622925, 4.54353666305542
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'log float32 positive 2D tensor',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [
            63.82542037963867,  25.317724227905273, 96.44790649414062,
            40.91856384277344,  36.579071044921875, 57.81629943847656,
            10.057244300842285, 17.836828231811523, 50.79246520996094,
            83.860595703125,    12.065509796142578, 22.702478408813477,
            47.559814453125,    17.543880462646484, 32.65243911743164,
            20.353010177612305, 52.54472351074219,  45.608802795410156,
            30.385812759399414, 13.709558486938477, 10.396759986877441,
            50.840946197509766, 5.682034492492676,  94.02275848388672
          ],
          'descriptor': {'dimensions': [4, 6], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [
            4.15615177154541,   3.2315046787261963, 4.569003105163574,
            3.7115838527679443, 3.5994763374328613, 4.057270526885986,
            2.308293104171753,  2.88126540184021,   3.927747964859009,
            4.4291558265686035, 2.4903509616851807, 3.122474193572998,
            3.861988067626953,  2.8647050857543945, 3.48591947555542,
            3.0132288932800293, 3.9616646766662598, 3.820100784301758,
            3.413975715637207,  2.618093252182007,  2.34149432182312,
            3.9287021160125732, 1.7373093366622925, 4.54353666305542
          ],
          'descriptor': {'dimensions': [4, 6], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'log float32 positive 3D tensor',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [
            63.82542037963867,  25.317724227905273, 96.44790649414062,
            40.91856384277344,  36.579071044921875, 57.81629943847656,
            10.057244300842285, 17.836828231811523, 50.79246520996094,
            83.860595703125,    12.065509796142578, 22.702478408813477,
            47.559814453125,    17.543880462646484, 32.65243911743164,
            20.353010177612305, 52.54472351074219,  45.608802795410156,
            30.385812759399414, 13.709558486938477, 10.396759986877441,
            50.840946197509766, 5.682034492492676,  94.02275848388672
          ],
          'descriptor': {'dimensions': [2, 3, 4], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [
            4.15615177154541,   3.2315046787261963, 4.569003105163574,
            3.7115838527679443, 3.5994763374328613, 4.057270526885986,
            2.308293104171753,  2.88126540184021,   3.927747964859009,
            4.4291558265686035, 2.4903509616851807, 3.122474193572998,
            3.861988067626953,  2.8647050857543945, 3.48591947555542,
            3.0132288932800293, 3.9616646766662598, 3.820100784301758,
            3.413975715637207,  2.618093252182007,  2.34149432182312,
            3.9287021160125732, 1.7373093366622925, 4.54353666305542
          ],
          'descriptor': {'dimensions': [2, 3, 4], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'log float32 positive 4D tensor',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [
            63.82542037963867,  25.317724227905273, 96.44790649414062,
            40.91856384277344,  36.579071044921875, 57.81629943847656,
            10.057244300842285, 17.836828231811523, 50.79246520996094,
            83.860595703125,    12.065509796142578, 22.702478408813477,
            47.559814453125,    17.543880462646484, 32.65243911743164,
            20.353010177612305, 52.54472351074219,  45.608802795410156,
            30.385812759399414, 13.709558486938477, 10.396759986877441,
            50.840946197509766, 5.682034492492676,  94.02275848388672
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [
            4.15615177154541,   3.2315046787261963, 4.569003105163574,
            3.7115838527679443, 3.5994763374328613, 4.057270526885986,
            2.308293104171753,  2.88126540184021,   3.927747964859009,
            4.4291558265686035, 2.4903509616851807, 3.122474193572998,
            3.861988067626953,  2.8647050857543945, 3.48591947555542,
            3.0132288932800293, 3.9616646766662598, 3.820100784301758,
            3.413975715637207,  2.618093252182007,  2.34149432182312,
            3.9287021160125732, 1.7373093366622925, 4.54353666305542
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'log float32 positive 5D tensor',
    'graph': {
      'inputs': {
        'logInput': {
          'data': [
            63.82542037963867,  25.317724227905273, 96.44790649414062,
            40.91856384277344,  36.579071044921875, 57.81629943847656,
            10.057244300842285, 17.836828231811523, 50.79246520996094,
            83.860595703125,    12.065509796142578, 22.702478408813477,
            47.559814453125,    17.543880462646484, 32.65243911743164,
            20.353010177612305, 52.54472351074219,  45.608802795410156,
            30.385812759399414, 13.709558486938477, 10.396759986877441,
            50.840946197509766, 5.682034492492676,  94.02275848388672
          ],
          'descriptor': {'dimensions': [2, 1, 4, 1, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'log',
        'arguments': [{'input': 'logInput'}],
        'outputs': 'logOutput'
      }],
      'expectedOutputs': {
        'logOutput': {
          'data': [
            4.15615177154541,   3.2315046787261963, 4.569003105163574,
            3.7115838527679443, 3.5994763374328613, 4.057270526885986,
            2.308293104171753,  2.88126540184021,   3.927747964859009,
            4.4291558265686035, 2.4903509616851807, 3.122474193572998,
            3.861988067626953,  2.8647050857543945, 3.48591947555542,
            3.0132288932800293, 3.9616646766662598, 3.820100784301758,
            3.413975715637207,  2.618093252182007,  2.34149432182312,
            3.9287021160125732, 1.7373093366622925, 4.54353666305542
          ],
          'descriptor': {'dimensions': [2, 1, 4, 1, 3], 'dataType': 'float32'}
        }
      }
    }
  }
];

if (navigator.ml) {
  logTests.forEach((test) => {
    webnn_conformance_test(
        buildGraphAndCompute, getLogPrecisionTolerance, test);
  });
} else {
  test(() => assert_implements(navigator.ml, 'missing navigator.ml'));
}
